Public Module Utilities

    Public Sub CreateAllSummaryClasses(ByVal outputDir As String)

        Using ctrl As New EntrezController

            For Each db As String In ctrl.GetDatabases

                CreateNewSummaryClass(db, IO.Path.Combine(outputDir, GetCapitalDbName(db) & "Summary.vb"))

            Next
        End Using

    End Sub


    Private Function GetCapitalDbName(ByVal dbName As String) As String

        Return dbName.Substring(0, 1).ToUpper & dbName.Substring(1, dbName.Length - 1)

    End Function

    Public Sub CreateNewSummaryClass(ByVal dbName As String, ByVal outputPath As String)

        Using ctrl As New EntrezController

            Dim results As IDataReader

            results = ctrl.SearchAllFields(dbName, "health OR life OR protein", 0, 5)

            Dim docSum As eUtils.DocSumType

            If results.RecordsAffected > 0 Then

                'Need to check for varying result type field counts and use GetFieldIndex if they 
                'are not consistent otherwise use the enum for index parameters
                'use result with longest field count to build methods and enum

                docSum = CType(results.Item(0), eUtils.DocSumType)

                Dim classBuilder As New Text.StringBuilder
                Dim methodBuilder As New Text.StringBuilder
                Dim enumBuilder As New Text.StringBuilder

                classBuilder.AppendLine(My.Resources.DataBaseSummaryTemplate)

                Dim capDbName As String

                capDbName = GetCapitalDbName(dbName)

                classBuilder.Replace("[DATABASE]", capDbName)

                Dim index As Integer = 0

                For Each item As eUtils.ItemType In docSum.Item

                    Dim fieldName As String = item.Name.Replace("_", String.Empty)


                    Select Case fieldName.ToLower

                        Case "date"
                            fieldName = "DateField"

                        Case "list"


                        Case "name"
                            fieldName = "NameField"

                        Case "alias"
                            fieldName = "AliasField"


                    End Select

                    Dim methodName As String = item.Type.ToString
                    Dim returnType As String = item.Type.ToString

                    Select Case item.Type
                        Case eUtils.ItemTypeType.Date
                            methodName = "DateTime"
                            returnType = "String"


                        Case eUtils.ItemTypeType.Enumerator

                        Case eUtils.ItemTypeType.Flags

                        Case eUtils.ItemTypeType.Integer

                        Case eUtils.ItemTypeType.List
                            returnType = "String()"


                        Case eUtils.ItemTypeType.Qualifier

                        Case eUtils.ItemTypeType.String

                        Case eUtils.ItemTypeType.Structure
                            methodName = "Structure"
                            returnType = "Object"

                        Case eUtils.ItemTypeType.Unknown


                    End Select



                    methodBuilder.Append(vbTab & "Public Function Get" & fieldName)

                    methodBuilder.AppendLine("() As " & returnType)

                    methodBuilder.AppendLine()

                    'If result types vary then
                    'methodBuilder.AppendLine(vbTab & vbTab & "Return MyBase.Get" & methodName & _
                    '    "(Me.GetFieldIndex(ItemTypeField." & fieldName & ".ToString))")
                    'Else
                    methodBuilder.AppendLine(vbTab & vbTab & "Return MyBase.Get" & methodName & _
                        "(ItemTypeField." & fieldName & ")")


                    methodBuilder.AppendLine()

                    methodBuilder.AppendLine(vbTab & "End Function")

                    methodBuilder.AppendLine()


                    enumBuilder.AppendLine(vbTab & vbTab & fieldName & " = " & index.ToString)

                    index += 1


                Next


                classBuilder.Replace("[ENUM]", enumBuilder.ToString)

                classBuilder.Replace("[MEMBERS]", methodBuilder.ToString)


                Using writer As IO.StreamWriter = IO.File.CreateText(outputPath)

                    writer.Write(classBuilder.ToString)

                End Using


            End If

        End Using


    End Sub

End Module
